import requests
from lxml import etree
from moviepy.editor import *
import re
import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

import time
def bilibilivideo(url):
    headers={
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36',
        'referer': url ,
    }
    proxies = {
        'http': '180.103.181.10:80',
    }
    response = requests.get(url, headers=headers)

    path = etree.HTML(response.text)
    title = path.xpath('//*[@id="viewbox_report"]/div[1]/div[1]/h1/text()')[0]
    print(title)
    #视频获取 4K:120 1080p:80 720p:64 480p:32 360p:16
    get_vd = re.findall(r'"id":32,"baseUrl":"(.*?)","base_url"',response.content.decode())
    print(get_vd[0])
    url_vd = requests.get(get_vd[0], headers=headers)
    print(url_vd.status_code)
    #音频获取
    get_ad = re.findall(r'"id":30280,"baseUrl":"(.*?)","base_url"',response.content.decode())
    print(get_ad[0])
    url_ad = requests.get(get_ad[0], headers=headers)
    print(url_ad.status_code)

    return title,get_vd[0],get_ad[0]
def advddown(title,get_vd,get_ad,filename,):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36',
        'referer': 'https://www.bilibili.com/',
    }
    proxies = {
        'http': '182.34.103.130:9999',
        'http': '182.34.103.130:9999',
        'http': '182.34.103.130:9999',
    }
    #音视频下载
    url_vd = requests.get(get_vd, headers=headers,proxies=proxies)
    print(url_vd.status_code)
    url_ad = requests.get(get_ad, headers=headers,proxies=proxies)
    print(url_ad.status_code)
    if os.path.exists('../临时'):
        pass
    else:
        os.mkdir('../临时')
    with open(f"../临时/vd.mp4","wb") as f:
        f.write(url_vd.content)
    print('video下载成功')
    with open(f"../临时/ad.mp4","wb") as f:
        f.write(url_ad.content)
    print('audio下载成功')
    # 合成
    vd = VideoFileClip(f'../临时/vd.mp4')
    ad = AudioFileClip(f'../临时/ad.mp4')
    vd2 = vd.set_audio(ad)
    vd2.write_videofile(f'{filename}.mp4')
    print("合成成功")
    # shutil.move(f'{filename}','视频/')
    # print("移动成功")
    os.remove(f'../临时/vd.mp4')
    print("删除video成功")
    os.remove(f'../临时/ad.mp4')
    print("删除audio成功")
# bilibilivideo('https://www.bilibili.com/video/BV1zZdWY2Efx/?spm_id_from=333.1007.tianma.1-1-1.click&vd_source=eb47fd60af3a7d2775f035f176abc156')
def bilibilicover(url):
    s = Service('../chromedriver.exe')
    # 配置浏览器选项
    options = Options()
    options.add_argument("--headless")  # 无头模式
    driver = webdriver.Chrome(service=s, options=options)
    driver.get(url)
    # 等待页面加载完成
    time.sleep(5)
    page = driver.page_source
    driver.quit()
    coverurl = re.findall(r'img src="(.*?)" alt', page)
    print(coverurl)
    if coverurl:
        print("封面 URL:", coverurl[0])
    else:
        print("未找到封面 URL")
    if os.path.exists('../临时'):
        pass
    else:
        os.mkdir('../临时')
    imgname = 0
    imgurl=requests.get('http:'+coverurl[0])
    with open(f'../临时/cover{imgname}.jpg', 'wb') as f:
        f.write(imgurl.content)
    return 'http:'+coverurl[0]
# bilibilicover('https://www.bilibili.com/video/BV1zZdWY2Efx/?spm_id_from=333.1007.tianma.1-1-1.click&vd_source=eb47fd60af3a7d2775f035f176abc156')
# title,vd,ad=bilibilivideo('https://www.bilibili.com/video/BV1cxZGYoEfV/?spm_id_from=333.1007.tianma.2-1-3.click&vd_source=eb47fd60af3a7d2775f035f176abc156')
# filename='C:\\Users\\15442\Desktop'
# advddown(title,vd,ad,filename)
# title,vd,ad=bilibilivideo('https://www.bilibili.com/video/BV1MGoLYgEwC/?spm_id_from=333.1007.tianma.2-2-5.click&vd_source=eb47fd60af3a7d2775f035f176abc156')
# advddown(title,vd,ad,filename='C:/Users/15442/Desktop')